package osm;
     
     
     
import geocoding.Converter;
import geocoding.CustomNodes;
import geocoding.DistanceCalculator;
import graph.ALGOBuslineEdge;
import graph.ALGOPlacemarkNode;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import urnav.example.com.R;
import urnav.example.com.URNavActivity;
import android.content.res.XmlResourceParser;
import android.util.Log;

   
     
    public class TagReader {

    	String TAG = "URNav";
    	String currentNodeID;
    	ArrayList <String> refList = new ArrayList <String> ();
    	ArrayList <String> tagList = new ArrayList <String> ();
    	ArrayList <String> valueList = new ArrayList <String> ();
    	String currentTag; 
    
//    	Reads the XML- file and sets facility- related variables in the CustomNodes
    public void readTags(CustomNodes cn){
    	
//    	Log.i(TAG, String.valueOf(cn.getSize()));
    	
        try
        
        {

//        		Initialize the XMLResourceParser
        		
                XmlResourceParser xrp = URNavActivity.actVar.getResources().getXml(R.xml.unix);

                while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
                	



                        if (xrp.getEventType() == XmlResourceParser.START_TAG) {

                			   
                           if (xrp.getName().equals("node")) {
                			
               
                			currentNodeID=xrp.getAttributeValue(null, "id");
//                			Log.i(TAG, "CNID: "  + currentNodeID);
//                			xrp.next();
                			}
                               
                               
                		   if (xrp.getName().equals("tag")){
                			   cn.setTag(cn.getPosition(currentNodeID), xrp.getAttributeValue(null, "k"));
                			   cn.setValue(cn.getPosition(currentNodeID), xrp.getAttributeValue(null, "v"));
                			   cn.setHas_tags(cn.getPosition(currentNodeID), true);
//                			   Log.i(TAG, "setHasTags true from Tag für " + currentNodeID);

                			xrp.next();
                			

                		   }	 
                		   
                           if (xrp.getName().equals("way")) {

                           	xrp.next();
                           	

                           while (!(xrp.getName().equals("way"))){
                           	if (xrp.getName().equals("nd")) {
//                           		Log.i(TAG, "IN WAY");
                                                                     
                           		refList.add((xrp.getAttributeValue(null, "ref")));

                           		xrp.next();
                           	
                           	}
                           	
                            
                            if (xrp.getName().equals("tag")){
                            	
                            	tagList.add(xrp.getAttributeValue(null, "k"));
                            	valueList.add(xrp.getAttributeValue(null, "v"));
                    				
                    				
                    				xrp.next();
                    			
                            }
                           	
                           	
                           	xrp.next();

                           	}
                           
                           

                           if (tagList.size()>0){
                           for (int i=0; i < refList.size(); i++){
                        	   cn.setHas_tags(cn.getPosition(refList.get(i)), true);
//                        	   Log.i(TAG, "setHas_tags TRUE from Way für " + refList.get(i));
                        	   for (int j=0; j < tagList.size(); j++){
                        		   cn.setTag(cn.getPosition(refList.get(i)), tagList.get(j));
//                        		Log.i(TAG, "Set Tag: " + tagList.get(j));   
                        		   cn.setValue(cn.getPosition(refList.get(i)), valueList.get(j));
//                        		Log.i(TAG, "Set Value: " + valueList.get(j));
                        	   }
                        	   
                           }
                           }

                           refList.clear();
                           tagList.clear();
                           valueList.clear();
//                           xrp.next();
                           	
                         	  
                         	 

                           }


                        }
                        

                        
                        else if (xrp.getEventType() == XmlResourceParser.END_TAG) {

                                ;

                        } 
                        
                        else if (xrp.getEventType() == XmlResourceParser.TEXT) {



                        }

                        xrp.next();

                }

                xrp.close();



        } catch (XmlPullParserException xppe) {

                Log.e(TAG, "Failure of .getEventType or .next, probably bad file format");

                xppe.toString();

        } catch (IOException ioe) {

                Log.e(TAG, "Unable to read resource file");

                ioe.printStackTrace();

        }


		}
    

    
    
    }